package com.zsc.shixun.controller;

import com.zsc.shixun.common.CommonResult;
import com.zsc.shixun.common.ResultUtil;
import com.zsc.shixun.config.UserSecurity;
import com.zsc.shixun.service.function.DataService;
import com.zsc.shixun.utils.LoginMessageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 数据观星
 * </p>
 *
 * @author ZWYZY
 * @since 2020/6/7
 */
@Controller
@RequestMapping("/data")
public class DataController {
    private static final Logger logger = LoggerFactory.getLogger(DataController.class);
    @Autowired
    private LoginMessageUtils loginMessageUtils;
    @Autowired
    private DataService dataService;

    @RequestMapping("/index")
    public String index(HttpServletRequest request) {
        UserSecurity userDetails = loginMessageUtils.getMessage();
        //1管理员，0为普通用户
        if (userDetails.getLoginUser().getRoleId() == 1) {
            request.setAttribute("dataVO", dataService.getDataByAdmin());
        } else {
            request.setAttribute("dataVO", dataService.getDataByCommon(userDetails.getLoginUser().getId()));
        }
        return "admin/data/index";
    }

    /**
     * 获取数据统计
     *
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    @Secured(value = "ROLE_data_select")
    public CommonResult getData() {
        UserSecurity userDetails = loginMessageUtils.getMessage();
        //1管理员，0为普通用户
        if (userDetails.getLoginUser().getRoleId() == 1) {
            return ResultUtil.success(dataService.getDataByAdmin());
        } else {
            return ResultUtil.success(dataService.getDataByCommon(userDetails.getLoginUser().getId()));
        }
    }
}
